package com.yuanli.cosmeticmirror.view.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.comenjoysoft.baselibrary.util.AppUtils;
import com.comenjoysoft.baselibrary.util.SPUtils;
import com.kongzue.dialog.v3.CustomDialog;
import com.yuanli.cosmeticmirror.R;
import com.yuanli.cosmeticmirror.base.BaseActivity;
import com.yuanli.cosmeticmirror.base.Constant;
import com.yuanli.cosmeticmirror.base.MyApp;
import com.yuanli.cosmeticmirror.model.iview.IUserView;
import com.yuanli.cosmeticmirror.persenter.UserPersenter;

import butterknife.BindView;
import butterknife.OnClick;
import comenjoy.com.imageloadlibrary.GlideUtil;

import static com.yuanli.cosmeticmirror.base.Constant.USER_AGREEMENT_PRIVACY_POLICY;

/**
 * 个人中心界面
 */
public class UserInfoActivity extends BaseActivity implements IUserView {
    @BindView(R.id.user_img)
    ImageView userImg;
    @BindView(R.id.user_back)
    ImageView userBack;
    @BindView(R.id.user_name)
    TextView userName;
    @BindView(R.id.user_favorite)
    LinearLayout userFavorite;
    @BindView(R.id.user_photo)
    LinearLayout userPhoto;
    @BindView(R.id.user_member)
    LinearLayout userMember;
    @BindView(R.id.user_is_member)
    ImageView userIsMember;
    @BindView(R.id.logout)
    TextView logout;
    @BindView(R.id.me_feedback)
    TextView meFeedback;
    @BindView(R.id.user_app_version)
    TextView userAppVersion;
    @BindView(R.id.user_check_update)
    LinearLayout userCheckUpdate;
    private long mLastClickTime = 0;
    public final long TIME_INTERVAL = 1000L;
    private UserPersenter persenter;

    @Override
    protected int getContentView() {
        return R.layout.activity_user;
    }

    @Override
    protected void init(Bundle bundle) {
        persenter = new UserPersenter(this, this);
    }

    @Override
    protected void initView() {
        userIsMember.setVisibility(MyApp.IsVIP ? View.VISIBLE : View.GONE);
        String version = getResources().getString(R.string.app_current_version_text);
        version = String.format(version, AppUtils.getAppVersionName(MyApp.getInstance()));
        userAppVersion.setText(version);
        String photo = SPUtils.getInstance().getString(Constant.SP_USER_PHOTO_NET);
        userName.setText(SPUtils.getInstance().getString(Constant.SP_USER_NAME));
        if (TextUtils.isEmpty(photo)) {
            GlideUtil.getInstance().loadCircleImage(UserInfoActivity.this, R.drawable.default_head_img, userImg);
        } else {
            GlideUtil.getInstance().loadCircleImage(UserInfoActivity.this, photo, R.drawable.default_head_img, userImg);
        }
    }

    @Override
    protected void initData() {

    }

    @Override
    protected void destroy() {

    }


    @OnClick({R.id.user_favorite, R.id.user_back, R.id.user_photo, R.id.user_member, R.id.logout, R.id.user_logoff, R.id.me_feedback, R.id.me_user_arg, R.id.me_privacy_policy, R.id.user_check_update})
    public void onViewClicked(View view) {
        long nowTime = System.currentTimeMillis();
        if (nowTime - mLastClickTime < TIME_INTERVAL) {
            // do something
            return;
        }
        mLastClickTime = nowTime;
        switch (view.getId()) {
            case R.id.user_favorite:
                startActivity(new Intent(UserInfoActivity.this, FavoriteActivity.class));
                break;
            case R.id.user_back:
                finish();
                break;
            case R.id.user_photo:
                startActivity(new Intent(UserInfoActivity.this, PhotoActivity.class));
                break;
            case R.id.user_member:
                startActivity(new Intent(UserInfoActivity.this, MemberActivity.class));
                break;
            case R.id.me_feedback:
                startActivity(new Intent(UserInfoActivity.this, FeedbackActivity.class));
                break;
            case R.id.user_check_update:
                break;
            case R.id.me_user_arg:
                startActivity(new Intent(UserInfoActivity.this, ProtocolActivity.class).putExtra(USER_AGREEMENT_PRIVACY_POLICY, 1));
                break;
            case R.id.me_privacy_policy:
                startActivity(new Intent(UserInfoActivity.this, ProtocolActivity.class).putExtra(USER_AGREEMENT_PRIVACY_POLICY, 2));

                break;
            case R.id.logout:
                getCustomDialog();
                break;
            case R.id.user_logoff:
                getCustomDialogLogOff();
                break;
            default:
                break;
        }
    }


    /**
     * 退出登录
     *
     * @return
     */
    private CustomDialog getCustomDialog() {
        return CustomDialog.show(UserInfoActivity.this, R.layout.dialog_logout, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.logout_enter);
            TextView btncancel = v.findViewById(R.id.logout_cancel);
            btnOk.setOnClickListener(v1 -> {
                startActivity(new Intent(UserInfoActivity.this, LoginActivity.class));
                MyApp.isLogin = false;
                MyApp.POSITION = -1;
                MyApp.IsVIP = false;
                MyApp.getPhotoFrameStatus().fragmentIndex = -1;
                MyApp.getPhotoFrameStatus().checkPosition = -1;
                //                SPUtils.getInstance().remove(Constant.SP_USER_NAME);
                //                SPUtils.getInstance().remove(Constant.SP_USER_PASSWORD);
                //                SPUtils.getInstance().remove(Constant.IS_FIRST_LOGIN);
                //                SPUtils.getInstance().remove(Constant.SP_USER_NAME);
                //                SPUtils.getInstance().remove(Constant.SP_USER_PHOTO_NET);
                SPUtils.getInstance().clear();
                appManager.finishActivity(MainActivity.class);
                finish();
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> dialog.doDismiss());
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }

    /**
     * 注销账号
     *
     * @return
     */
    private CustomDialog getCustomDialogLogOff() {
        return CustomDialog.show(UserInfoActivity.this, R.layout.dialog_logoff, (dialog, v) -> {
            TextView btnOk = v.findViewById(R.id.logoff_enter);
            TextView btncancel = v.findViewById(R.id.logoff_cancel);
            btnOk.setOnClickListener(v1 -> {
                showLoadDialog();
                persenter.logoff();
                dialog.doDismiss();
            });
            btncancel.setOnClickListener(v1 -> dialog.doDismiss());
        }).setAlign(CustomDialog.ALIGN.DEFAULT);
    }

    @Override
    public void onSucess(int code) {
        dialogDismiss();
        if (code == 1) {
            showToast("注销成功");
            startActivity(new Intent(UserInfoActivity.this, LoginActivity.class));
            MyApp.isLogin = false;
            MyApp.POSITION = -1;
            MyApp.IsVIP = false;
            MyApp.getPhotoFrameStatus().fragmentIndex = -1;
            MyApp.getPhotoFrameStatus().checkPosition = -1;
            SPUtils.getInstance().remove(Constant.SP_USER_NAME);
            SPUtils.getInstance().remove(Constant.SP_USER_PASSWORD);
            SPUtils.getInstance().remove(Constant.IS_FIRST_LOGIN);
            SPUtils.getInstance().remove(Constant.SP_USER_NAME);
            SPUtils.getInstance().remove(Constant.SP_USER_PHOTO_NET);
            appManager.finishActivity(MainActivity.class);
            finish();
        }
    }

    @Override
    public void onError(String msg) {
        dialogDismiss();
        showToast(msg);
    }
}
